<template>
  <div class="chartNum">
    <div class="chartNum_bj">
      <img src="./../../assets/img/index/datav_money_bj.png" alt />
    </div>
    <div class="chartNum_content">
      <span class="chartNum_title">营收：</span>
      <div class="box-item" v-if="revenue">
        <li :class="{'number-item': !isNaN(item), 'mark-item': isNaN(item) }" v-for="(item,index) in revenue.split('')" :key="index">
          <span v-if="!isNaN(item)">
            <i ref="numberItem">0123456789</i>
          </span>
          <span class="comma" v-else>{{item}}</span>
        </li>
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        newNumber: 9999,
        orderNum: ["0", "0", ",", "0", "0", "0", ",", "0", "0", "0"] // 默认订单总数
      };
    },
    props: {
      revenue: {
        type: String,
        default: ""
      }
    },
    mounted() {
      var that = this;
      this.$nextTick(() => {
        this.increaseNumber();
        // this.increaseNumber();
      });
    },
    methods: {
      increaseNumber() {
        let self = this;
        this.timer = setInterval(() => {
          self.newNumber = self.newNumber + self.getRandomNumber(1, 100);
          self.setNumberTransform();
          this.toOrderNum(this.newNumber);
        }, 1000);
      },
      getRandomNumber(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
      },
      // 设置文字滚动
      setNumberTransform() {
        const numberItems = this.$refs.numberItem; // 拿到数字的ref，计算元素数量
        const numberArr = this.revenue.split('').filter(item => !isNaN(item));
        // 结合CSS 对数字字符进行滚动,显示订单数量
        for (let index = 0; index < numberItems.length; index++) {
          const elem = numberItems[index];
          elem.style.transform = `translate(-50%, -${numberArr[index] * 10}%)`;
        }
      },
      // 处理总订单数字
      toOrderNum(num) {
        num = num.toString();
        // 把订单数变成字符串
        // console.log("iiiiiiiiiiiiiii:",num)
        if (num.length < 8) {
          num = "0" + num; // 如未满八位数，添加"0"补位
          this.toOrderNum(num); // 递归添加"0"补位
        } else if (num.length === 8) {
          // 订单数中加入逗号
          num = num.slice(0, 2) + "," + num.slice(2, 5) + "," + num.slice(5, 8);
          this.orderNum = num.split(""); // 将其便变成数据，渲染至滚动数组
        } else {
          // 订单总量数字超过八位显示异常
          this.$message.warning("订单总量数字过大，显示异常，请联系客服");
        }
      }
    },
    // created() {
    //   this.orderNum = this.revenue.split("");
    //   console.log("kkkkkkkkkkkkk:",this.orderNum)
    // }
    // watch: {
    //   revenue(value){
    //     this.orderNum = value.split("");
    //     console.log("kkkkkkkkkkkkk:",this.orderNum)
    //   }
    // }
  };
</script>
<style scoped lang='less'>
  .chartNum {
    width: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    position: relative;

    .chartNum_bj {
      width: 100%;

      img {
        // width: 100%;
        height: 100%;
      }
    }

    .chartNum_content {
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      padding: 0 10px;
    }
  }

  .chartNum_title {
    width: 18%;

    color: #ffffff;
    font-weight: bold;

    @media only screen and (min-width: 1680px) and (max-width: 1920px) {
      font-size: 24px;
    }

    @media only screen and (min-width: 1440px) and (max-width: 1680px) {
      font-size: 20px;
    }

    @media only screen and (min-width: 1024px) and (max-width: 1440px) {
      font-size: 16px;
    }

    @media only screen and (max-width: 1024px) {
      font-size: 14px;
    }
  }

  /*订单总量滚动数字设置*/
  .box-item {
    width: 82%;
    flex: 1;
    position: relative;
    // height: 100px;
    font-size: 1.9em;
    line-height: 1;
    text-align: center;
    list-style: none;
    color: #2d7cff;
    writing-mode: vertical-lr;
    text-orientation: upright;
    /*文字禁止编辑*/
    -moz-user-select: none;
    /*火狐*/
    -webkit-user-select: none;
    /*webkit浏览器*/
    -ms-user-select: none;
    /*IE10*/
    -khtml-user-select: none;
    /*早期浏览器*/
    user-select: none;
    /* overflow: hidden; */
  }

  /* 默认逗号设置 */
  .mark-item {
    width: 10px;
    // height: 100px;
    margin-right: 5px;
    line-height: 10px;
    font-size: 1.2em;
    position: relative;

    &>span {
      position: absolute;
      width: 100%;
      bottom: 0;
      writing-mode: vertical-rl;
      text-orientation: upright;
    }
  }

  /*滚动数字设置*/
  .number-item {
    width: 9.5%;

    @media only screen and (min-width: 1024px) and (max-width: 1440px) {
      width: 9%;
    }

    @media only screen and (max-width: 1024px) {
      font-size: 14px;
    }

    height: 40px;
    background: #ccc;
    list-style: none;
    margin-right: 7px;
    background: rgba(250, 250, 250, 1);
    border-radius: 4px;
    // border: 1px solid rgba(221, 221, 221, 1);
    background: url("./../../assets/img/index/datav_money.png") no-repeat;
    background-size: 100% 100%;

    &>span {
      position: relative;
      display: inline-block;
      margin-right: 10px;
      width: 100%;
      height: 100%;
      writing-mode: vertical-rl;
      text-orientation: upright;
      overflow: hidden;

      &>i {
        font-style: normal;
        position: absolute;
        top: 6px;
        left: 50%;
        transform: translate(-50%, 0);
        transition: transform 1s ease-in-out;
        letter-spacing: 10px;
      }
    }
  }

  .number-item:last-child {
    margin-right: 0;
  }
</style>
